package com.sky.Task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ClassName:OrderTasj
 * Package: com.sky.Task
 * Description:
 *
 * @Author ZCW
 * @Create 2024/12/17 9:57
 * @Version 1.0
 */

@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderMapper orderMapper;

    @Scheduled(cron = "0 * * * * ? ")
    public void processTimeOut() {
        log.info("定时处理超时订单{}", LocalDateTime.now());
        List<Orders> byStatusAndOrderTime = orderMapper.getByStatusAndOrderTime(Orders.TO_BE_CONFIRMED, LocalDateTime.now().plusMinutes(-15));

        if (byStatusAndOrderTime != null && byStatusAndOrderTime.size() != 0) {
            for (Orders o : byStatusAndOrderTime) {
                o.setStatus(Orders.CANCELLED);
                o.setCancelReason("订单超时，自动取消");
                o.setCancelTime(LocalDateTime.now());
                orderMapper.update(o);
            }
        }
    }

    @Scheduled(cron = "0 0 1 * * ?")
    public void prossDeliveryOrder() {
        log.info("定时处理处于派送中的订单", LocalDateTime.now());
        List<Orders> byStatusAndOrderTime = orderMapper.getByStatusAndOrderTime(Orders.DELIVERY_IN_PROGRESS, LocalDateTime.now().plusMinutes(-60));
        if (byStatusAndOrderTime != null && byStatusAndOrderTime.size() != 0) {
            for (Orders o : byStatusAndOrderTime) {
                o.setStatus(Orders.COMPLETED);
                orderMapper.update(o);
            }
        }
    }
}
